<?php
/**
 * @package     Joomla.Site
 * @subpackage  mod_articles_latest
 *
 * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

require_once JPATH_SITE . '/components/com_content/helpers/route.php';

JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_content/models', 'ContentModel');

/**
 * Helper for mod_articles_latest
 *
 * @package     Joomla.Site
 * @subpackage  mod_articles_latest
 */
abstract class ModArticlesFeaturedHelper
{
	public static function getList(&$params)
	{
		// Get the dbo
		$db = JFactory::getDbo();

		// Get an instance of the generic articles model
		$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));

		// Set application parameters in model
		$app = JFactory::getApplication();
		$appParams = $app->getParams();
		$model->setState('params', $appParams);

		// Set the filters based on the module params
		$model->setState('list.start', 0);
		$model->setState('list.limit', (int) $params->get('count', 5));
		$model->setState('filter.published', 1);

		// Access filter
		$access = !JComponentHelper::getParams('com_content')->get('show_noauth');
		$authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
		$model->setState('filter.access', $access);

		// Category filter
		$model->setState('filter.category_id', $params->get('catid', array()));

		$nullDate = $db->quote($db->getNullDate());
		$nowDate = $db->quote(JFactory::getDate()->toSQL());
		$query = $db->getQuery(true);
				
		$query->select('w.*, CASE WHEN CHAR_LENGTH(w.alias) != 0 THEN CONCAT_WS(\':\', w.id, w.alias) ELSE w.id END as slug')
		->from('#__content w')
		->where('w.state = 1 AND w.checked_out = 0 AND w.featured=1')			
		->where('(w.publish_up = ' . $nullDate . ' OR w.publish_up <= ' . $nowDate . ')')
		->where('(w.publish_down = ' . $nullDate . ' OR w.publish_down >= ' . $nowDate . ')')
		->order('w.created DESC');
		
		// Join to check for category published state in parent categories up the tree
		// If all categories are published, badcats.id will be null, and we just use the article state
		$subquery = ' (SELECT cat.id as id FROM #__categories AS cat JOIN #__categories AS parent ';
		$subquery .= 'ON cat.lft BETWEEN parent.lft AND parent.rgt ';
		$subquery .= 'WHERE parent.extension = ' . $db->quote('com_content');
		$subquery .= ' AND parent.published <= 0 GROUP BY cat.id)';
		
		$query->join('LEFT OUTER', $subquery . ' AS badcats ON badcats.id = w.id');
		
		// Filter by language
		if (JFactory::getApplication()->getLanguageFilter())
		{
			$query->where('language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
		}
		
		$db->setQuery($query, 0, (int) $params->get('count'));
		$rows = (array) $db->loadObjectList();
		
		$app = JFactory::getApplication();
		$menu = $app->getMenu();
						
		foreach ($rows as $row)
		{										
			if ($access || in_array($row->access, $authorised))
			{
				// We know that user has the privilege to view the article
				$row->link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catid));
			}
			else
			{
				$row->link = JRoute::_('index.php?option=com_users&view=login');
			}
												
		}
		return $rows;
		
	}
}
